package com.xjt.excel.function;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xjt.excel.baidu.AuthService;
import com.xjt.excel.util.BaseControllerUtil;
import com.xjt.excel.util.HttpUtil;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author xu
 * @Description
 * @createTime 2021年08月13日 10:00:00
 */

@RestController
public class test {

	final String URL = "https://aip.baidubce.com/rest/2.0/face/v3/detect";


	@PostMapping(value = "/facebaidu")
	public void testbaidu(HttpServletResponse response, HttpServletRequest request, MultipartFile file) throws IOException {
		String token = AuthService.getAuth();
		System.out.println(token);
		String baiduURL = URL+"?access_token="+token;
		System.out.println(baiduURL);
		Map<String,Object> param = new HashMap<>();
		String encode = Base64Utils.encodeToString(file.getBytes()).replaceAll(" ", "");
		System.out.println(encode);
		param.put("image", encode);
		param.put("image_type", "BASE64");
		param.put("face_field", "age,beauty,expression,face_shape,gender,glasses,landmark,landmark150,quality,eye_status,emotion,face_type,mask,spoofing");
		com.alibaba.fastjson.JSONObject finalParam = (com.alibaba.fastjson.JSONObject) JSON.toJSON(param);
		String s = HttpUtil.sendPostJson2(finalParam, baiduURL);
		Map map = JSONObject.parseObject(s, Map.class);
		System.out.println(map);
		BaseControllerUtil.renderResult(response, map);
	}

}
